﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using Utility;
using Utility.Service;

namespace Site
{
    public partial class Home : UserControl
    {
        private string uploadpath;

        private Storyboard storyFadeIn1;
        private Storyboard storyFadeOut1;
        private Storyboard storyFadeIn2;
        private Storyboard storyFadeOut2;
        private Storyboard storyFadeIn3;
        private Storyboard storyFadeOut3;
        private Storyboard storyFadeIn4;
        private Storyboard storyFadeOut4;

        public Home()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Home_Loaded);
        }

        /// <summary>
        /// 图标淡入效果
        /// </summary>
        /// <param name="fadein"></param>
        /// <param name="fadeout"></param>
        /// <param name="img"></param>
        private void FadeIn(ref Storyboard fadein,ref Storyboard fadeout, Image img,Canvas cns)
        {
            if (null != fadeout && fadeout.GetCurrentState() != ClockState.Stopped)
            {
                fadeout.Pause();
            }

            DoubleAnimation animation = new DoubleAnimation();
            animation.From = img.Opacity;
            animation.To = 1;
            animation.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation, img);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Image.OpacityProperty));

            DoubleAnimation animation1 = new DoubleAnimation();
            animation1.From = cns.Opacity;
            animation1.To = 1;
            animation1.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation1, cns);
            Storyboard.SetTargetProperty(animation1, new PropertyPath(Canvas.OpacityProperty));

            fadein = new Storyboard();
            fadein.Children.Add(animation);
            fadein.Children.Add(animation1);
            fadein.Begin();
        }

        /// <summary>
        /// 图片淡出效果
        /// </summary>
        /// <param name="fadein"></param>
        /// <param name="fadeout"></param>
        /// <param name="img"></param>
        private void FadeOut(ref Storyboard fadein, ref Storyboard fadeout, Image img,Canvas cns)
        {
            if (null != fadein && fadein.GetCurrentState() != ClockState.Stopped)
            {
                fadein.Pause();
            }
            fadein = null;

            DoubleAnimation animation = new DoubleAnimation();
            animation.From = img.Opacity;
            animation.To = 0;
            animation.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation, img);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Image.OpacityProperty));

            DoubleAnimation animation1 = new DoubleAnimation();
            animation1.From = cns.Opacity;
            animation1.To = 0;
            animation1.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation1, cns);
            Storyboard.SetTargetProperty(animation1, new PropertyPath(Canvas.OpacityProperty));

            fadeout = new Storyboard();
            fadeout.Children.Add(animation);
            fadeout.Children.Add(animation1);
            fadeout.Begin();
        }

        /// <summary>
        /// 外发光效果
        /// </summary>
        private DropShadowEffect EffectShadow()
        {
            System.Windows.Media.Effects.DropShadowEffect ds = new System.Windows.Media.Effects.DropShadowEffect();
            ds.Color = Colors.White;
            ds.BlurRadius = 100;
            ds.ShadowDepth = 0;
            return ds;
        }

        void Home_Loaded(object sender, RoutedEventArgs e)
        {
            //图标外发光效果
            DropShadowEffect effect=EffectShadow();
            imgTech1_1.Effect = effect;
            imgTech2_1.Effect = effect;
            imgTech3_1.Effect = effect;
            imgTech4_1.Effect = effect;

            //菜单下滑动画
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = -83;
            animation.To = 0;
            animation.Duration = new Duration(new TimeSpan(0, 0, 1));
            Storyboard.SetTarget(animation, myMenu);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Canvas.TopProperty));
            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(animation);
            storyboard.Begin();

            uploadpath = App.Current.Host.Source.AbsoluteUri.Substring(0, App.Current.Host.Source.AbsoluteUri.IndexOf("/ClientBin"));

            ServiceClient client = CUtility.GetServiceClient();
            client.ReadCommonBySortCompleted += new EventHandler<ReadCommonBySortCompletedEventArgs>(client_ReadCommonBySortCompleted);
            client.ReadCommonBySortAsync("1");
        }

        void client_ReadCommonBySortCompleted(object sender, ReadCommonBySortCompletedEventArgs e)
        {
            if (null == e.Error)
            {
                if (null != e.Result)
                {
                    BitmapImage img1 = new BitmapImage(new Uri(uploadpath + "/" + e.Result[0].Content));
                    Image itech1 = new Image();
                    itech1.Source = img1;
                    this.cnsTech1.Children.Add(itech1);

                    BitmapImage img2 = new BitmapImage(new Uri(uploadpath + "/" + e.Result[1].Content));
                    Image itech2 = new Image();
                    itech2.Source = img2;
                    this.cnsTech2.Children.Add(itech2);

                    BitmapImage img3 = new BitmapImage(new Uri(uploadpath + "/" + e.Result[2].Content));
                    Image itech3 = new Image();
                    itech3.Source = img3;
                    this.cnsTech3.Children.Add(itech3);

                    BitmapImage img4 = new BitmapImage(new Uri(uploadpath + "/" + e.Result[3].Content));
                    Image itech4 = new Image();
                    itech4.Source = img4;
                    this.cnsTech4.Children.Add(itech4);

                    this.LayoutRoot.UpdateLayout();
                }
            }
            else
            {
                throw e.Error;
            }
        }

        #region Tech1
        private void imgTech1_1_MouseEnter(object sender, MouseEventArgs e)
        {
            FadeIn(ref storyFadeIn1, ref storyFadeOut1, imgTech1_1, cnsTech1);
        }

        private void imgTech1_1_MouseLeave(object sender, MouseEventArgs e)
        {
            FadeOut(ref storyFadeIn1, ref storyFadeOut1, imgTech1_1, cnsTech1);
        }

        //private void imgTech1_MouseEnter(object sender, MouseEventArgs e)
        //{
        //    FadeIn(ref storyFadeIn1, ref storyFadeOut1, imgTech1_1, cnsTech1);
        //}

        //private void imgTech1_MouseLeave(object sender, MouseEventArgs e)
        //{
        //    FadeOut(ref storyFadeIn1, ref storyFadeOut1, imgTech1_1, cnsTech1);   
        //}
        #endregion

        #region Tech2

        private void imgTech2_1_MouseEnter(object sender, MouseEventArgs e)
        {
            FadeIn(ref storyFadeIn2, ref storyFadeOut2, imgTech2_1, cnsTech2);
        }

        private void imgTech2_1_MouseLeave(object sender, MouseEventArgs e)
        {
            FadeOut(ref storyFadeIn2, ref storyFadeOut2, imgTech2_1, cnsTech2);
        }

        //private void imgTech2_MouseEnter(object sender, MouseEventArgs e)
        //{
        //    FadeIn(ref storyFadeIn2, ref storyFadeOut2, imgTech2_1, cnsTech2);
        //}

        //private void imgTech2_MouseLeave(object sender, MouseEventArgs e)
        //{
        //    FadeOut(ref storyFadeIn2, ref storyFadeOut2, imgTech2_1, cnsTech2);
        //}
        #endregion

        #region Tech3
        private void imgTech3_1_MouseEnter(object sender, MouseEventArgs e)
        {
            FadeIn(ref storyFadeIn3, ref storyFadeOut3, imgTech3_1, cnsTech3);
        }

        private void imgTech3_1_MouseLeave(object sender, MouseEventArgs e)
        {
            FadeOut(ref storyFadeIn3, ref storyFadeOut3, imgTech3_1, cnsTech3);
        }

        //private void imgTech3_MouseEnter(object sender, MouseEventArgs e)
        //{
        //    FadeIn(ref storyFadeIn3, ref storyFadeOut3, imgTech3_1, cnsTech3);
        //}

        //private void imgTech3_MouseLeave(object sender, MouseEventArgs e)
        //{
        //    FadeOut(ref storyFadeIn3, ref storyFadeOut3, imgTech3_1, cnsTech3);
        //}
        #endregion

        #region Tech4
        private void imgTech4_1_MouseEnter(object sender, MouseEventArgs e)
        {
            FadeIn(ref storyFadeIn4, ref storyFadeOut4, imgTech4_1, cnsTech4);
        }

        private void imgTech4_1_MouseLeave(object sender, MouseEventArgs e)
        {
            FadeOut(ref storyFadeIn4, ref storyFadeOut4, imgTech4_1, cnsTech4);
        }

        //private void imgTech4_MouseEnter(object sender, MouseEventArgs e)
        //{
        //    FadeIn(ref storyFadeIn4, ref storyFadeOut4, imgTech4_1, cnsTech4);
        //}        

        //private void imgTech4_MouseLeave(object sender, MouseEventArgs e)
        //{
        //    FadeOut(ref storyFadeIn4, ref storyFadeOut4, imgTech4_1, cnsTech4);
        //}
        #endregion
    }
}
